package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import controller.Interface.GuiController;
import exceptions.UsernameException;

/**
 * <b>Project ChatSystemG6 INSA 4INFOA1 - 2013/2014</b>
 * <P>
 * <i>Package gui </i> <br>
 * WelcomePage is The logging page of the chatSystem <br>
 * it implements the interface ActionListener
 * 
 * @author Christelle SIAGO HAPPI
 * 
 */
public class WelcomePage extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JButton connectButton; // the connection button
	private JButton disconnectButton; // the disconnection button
	private JLabel usernameLabel; // the usesrname label
	private JTextField usernameText; // the username text field
	private JLabel welcomePageTitle; // the welcome page title
	private ImagePanel panel; // the panel of this frame
	private static int MIN_SIZE_USERNAME; // the minimum size of the username
	private static int MAX_SIZE_USERNAME; // the maximum size of the username
	private GuiController mCtrl; // the controller of this frame

	/**
	 * Constructor of the class WelcomePage
	 * 
	 * @param mCtrl
	 *            : the controller
	 */
	public WelcomePage(GuiController mCtrl) {

		this.mCtrl = mCtrl;
		initComponents();
		setTitle("ChatSystem Home");
		this.setSize(315, 500);
		this.setLocationRelativeTo(null);
		this.getContentPane().add(panel);
		this.setResizable(false);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		MIN_SIZE_USERNAME = 3;
		MAX_SIZE_USERNAME = 15;
		this.setBackground(Color.BLUE);

	}

	/**
	 * init the component of the logging interface
	 */
	private void initComponents() {

		// set an icon to the frame
		setIconImage(new ImageIcon("app.gif").getImage());

		// creation of the components
		connectButton = new JButton();
		disconnectButton = new JButton("disconnect");
		usernameLabel = new JLabel();
		usernameText = new JTextField(20);
		welcomePageTitle = new JLabel();

		// setting the usernameText component
		usernameText.setMaximumSize(usernameText.getPreferredSize());
		usernameText.setToolTipText("Enter your login");

		// allow to connect by pressing the input key
		usernameText.addKeyListener(new KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				try {
					logFieldKeyPressed(evt);
				} catch (UsernameException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});
		// setting the connectButton component
		connectButton.setText("Connect");
		connectButton.addActionListener(this);
		connectButton.setForeground(new Color(0, 153, 255));
		connectButton.setFont(new Font("Tahoma", 2, 11));

		disconnectButton.addActionListener(this);

		// setting the title of the logging frame
		welcomePageTitle.setText("ChatSystem Log");
		welcomePageTitle.setForeground(new Color(0, 153, 204));
		welcomePageTitle.setFont(new Font("Tahoma",0, 20));

		// setting the username label
		usernameLabel.setText("Username: ");
		usernameLabel.setForeground(new Color(0, 153, 204));

		// placing the components
		Box hBox1 = Box.createHorizontalBox();// creation of a horizontal block
												// to display the title
		hBox1.add(welcomePageTitle, BorderLayout.CENTER);
		hBox1.add(Box.createHorizontalStrut(40));

		Box hBox2 = Box.createHorizontalBox();// creation of a vertical block
												// to display the username
		hBox2.add(usernameLabel);
		hBox2.add(Box.createHorizontalStrut(5));
		hBox2.add(usernameText);

		Box hBox3 = Box.createHorizontalBox(); // block for connection
		hBox3.add(connectButton);
		hBox3.add(Box.createHorizontalStrut(5)); // added a 5 pixel space
		// between the component Horizontal

		Box vBox = Box.createVerticalBox(); // vertical block group
		vBox.add(hBox1);
		vBox.add(hBox2);

		vBox.add(Box.createGlue()); // adds a space between components
									// vertical
		vBox.add(hBox3);

		// setting the layout
		Container c = getContentPane();
		// insertion of the background image
		panel = new ImagePanel(new ImageIcon(
				"backgroundImage.jpg").getImage());
		panel.setLayout(null);

		c.add(vBox, BorderLayout.CENTER);
		vBox.setSize(300, 400);
		c.add(panel, BorderLayout.CENTER);
		panel.setOpaque(false);

		setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

		// displaying the frame
		this.setVisible(true);
		pack();

	}

	/**
	 * 
	 * @return the username of the local user
	 */
	public String getUsername() {
		String username = usernameText.getText();
		return username;
	}

	/**
	 * action Performed when the connect button is pressed
	 */
	@SuppressWarnings("static-access")
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(connectButton)) {

			if (getUsername().equals("")) {

				// Transmission information message
				JOptionPane jop1 = new JOptionPane();
				jop1.showMessageDialog(null, "please type your username",
						"Information", JOptionPane.INFORMATION_MESSAGE);

			} else if ((getUsername().length() < MIN_SIZE_USERNAME)) {

				// Transmission information message
				JOptionPane jop2 = new JOptionPane();
				jop2.showMessageDialog(null,
						"your username should be greater than 3 characters",
						"Information", JOptionPane.INFORMATION_MESSAGE);

			} else if (getUsername().length() > MAX_SIZE_USERNAME) {
				// Transmission information message
				JOptionPane jop3 = new JOptionPane();
				jop3.showMessageDialog(null,
						"your username should be lower than 15 characters",
						"Information", JOptionPane.INFORMATION_MESSAGE);
			} else {
				connect();
			}
		}

	}

	/**
	 * open the main Frame of the ChatSystem
	 */
	public void connect() {
		ChatGUI gui = new ChatGUI(this.mCtrl);
		gui.setWelcomePage(this);
		gui.connect();
		if (gui.connected()){
			this.usernameText.setText("");
		}
	

	}

	/**
	 * Manages the connection via the local user pressing the Enter key
	 * 
	 * @param evt
	 * @throws UsernameException
	 */
	private void logFieldKeyPressed(KeyEvent evt) throws UsernameException {// GEN-FIRST:event_logFieldKeyPressed
		if (evt.getKeyCode() == KeyEvent.VK_ENTER)
			connect();

	}

	public void setUsernameArea(String b){
		this.usernameText.setText("");
	}
}
